package com.example.project01.api;

import com.example.project01.model.Feedback;
import com.example.project01.service.FeedbackService;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.leon.model.I18nException;
import com.leon.model.Pageable;
import com.leon.web.annotation.ApiAuth;
import com.leon.web.annotation.ApiLogger;
import com.leon.web.annotation.ApiSetting;
import com.leon.web.annotation.Condition;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;


@Slf4j
@Validated
@RestController
@AllArgsConstructor
public class FeedbackApi {

    private final FeedbackService feedbackService;

    @GetMapping("/feedback")
    @Operation(description = "获取用户feedback")
    @ApiSetting(log = @ApiLogger(print = true), auth = @ApiAuth(condition = Condition.PERMITS))
    public ResponseEntity<?> feedbacks(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "20") Integer size,
                                       @RequestParam(value = "liaison", defaultValue = "") String liaison,
                                       @RequestParam(value = "state", defaultValue = "") String state,
                                       @RequestParam(value = "reactionAt", required = false)
                                           @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") Timestamp reactionAt,
                                       @RequestParam(value = "solveAt", required = false)
                                           @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") Timestamp solveAt) {
        return ResponseEntity.ok().body(feedbackService.feedback(Pageable.of(page, size), liaison, state, reactionAt, solveAt));
    }

    @PutMapping("/feedback")
    @Operation(description = "处理用户feedback")
    @ApiSetting(log = @ApiLogger(print = true), auth = @ApiAuth(condition = Condition.PERMITS))
    public ResponseEntity<?> updateFeedback(@RequestParam(value = "solveID") Long solveID) throws I18nException{
        Feedback feedback = new Feedback();
        feedback.setId(solveID);
        int result = feedbackService.save(feedback, true);
        if (result <= 0){
            throw new I18nException("feedback.error");
        }
        return ResponseEntity.ok().build();
    }
}
